package com.junzhiit.nengji.cms.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.junzhiit.nengji.cms.service.ShopInfoService;
import com.junzhiit.nengji.cms.web.exception.ShopInfoNotFoundException;
import com.junzhiit.nengji.cms.web.exception.ShopNotFoundException;
import com.junzhiit.nengji.cms.web.util.aliyun.AliyunOSSClientUtil;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.ShopInfoEditVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.dao.ShopDAO;
import com.junzhiit.nengji.dao.ShopInfoDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Shop;
import com.junzhiit.nengji.model.ShopInfo;
import com.junzhiit.nengji.model.enums.AuditStatus;
import com.junzhiit.nengji.model.enums.Is;
import com.junzhiit.nengji.model.enums.ShopStatus;

@Service
public class ShopInfoServiceImpl extends BaseServiceImpl implements ShopInfoService {

	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;

	@Autowired
	private ShopDAO shopDAO;
	
	@Autowired
	private ShopInfoDAO shopInfoDAO;
	
	@Value("${upload.temp}")
	private String uploadPath;
	
	@Value("${upload.mall.shop.info}")
	private String shopInfoPath;
	
	//阿里云API的bucket名称
	@Value("${oss.backet.name}")
	private String backetName;
	
	@Override
	public ResultVO findBySessionId(String sessionId){
		ResultVO result=new ResultVO();
		
		SystemUserSessionVO systemUserSessionVO = systemUserCacheDAO
				.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		Shop shop = shopDAO.query(systemUserSessionVO.getSystemId());
		if (shop == null) {
			throw new ShopNotFoundException("店铺不存在");
		}
		
		ShopInfo data=shopInfoDAO.findByShopId(shop.getId());
		result.setData(data);
		return result;
	}
	
	@Override
	public ResultVO update(ShopInfoEditVO shopInfoEditVO) {
		ResultVO result=new ResultVO();
		
		ShopInfo shopInfo=shopInfoDAO.query(shopInfoEditVO.getId());
		if(shopInfo==null){
			throw new ShopInfoNotFoundException("营业执照信息是不存在的");
		}
		
		ShopInfo newShopInfo=new ShopInfo();
		BeanUtils.copyProperties(shopInfoEditVO, newShopInfo);
		shopInfoDAO.update(newShopInfo);
		
		String identityFrontImgUrl=shopInfo.getIdentityFrontImgUrl();
		String newIdentityFrontImgUrl=shopInfoEditVO.getIdentityFrontImgUrl();
		if(StringUtils.isNotBlank(newIdentityFrontImgUrl) && !newIdentityFrontImgUrl.equals(identityFrontImgUrl)){
			//FileUtil.moveFile(uploadPath + newIdentityFrontImgUrl, shopInfoPath);
			boolean is = AliyunOSSClientUtil.moveFile(backetName,newIdentityFrontImgUrl, shopInfoPath, uploadPath);
			if(is){
				AliyunOSSClientUtil.removeFile(backetName, uploadPath+newIdentityFrontImgUrl);
			}
		}
		
		String identityBackImgUrl=shopInfo.getIdentityBackImgUrl();
		String newIdentityBackImgUrl=shopInfoEditVO.getIdentityBackImgUrl();
		if(StringUtils.isNotBlank(newIdentityBackImgUrl) && !newIdentityBackImgUrl.equals(identityBackImgUrl)){
			//FileUtil.moveFile(uploadPath + newIdentityBackImgUrl, shopInfoPath);	
			boolean is = AliyunOSSClientUtil.moveFile(backetName,newIdentityBackImgUrl, shopInfoPath, uploadPath);
			if(is){
				AliyunOSSClientUtil.removeFile(backetName, uploadPath+newIdentityBackImgUrl);
			}
		}
		
		
		String identityImgUrl=shopInfo.getIdentityImgUrl();
		String newIdentityImgUrl=shopInfoEditVO.getIdentityImgUrl();
		if(StringUtils.isNotBlank(newIdentityImgUrl) && !newIdentityImgUrl.equals(identityImgUrl)){
			//FileUtil.moveFile(uploadPath + newIdentityImgUrl, shopInfoPath);
			boolean is = AliyunOSSClientUtil.moveFile(backetName,newIdentityImgUrl, shopInfoPath, uploadPath);
			if(is){
				AliyunOSSClientUtil.removeFile(backetName, uploadPath+newIdentityImgUrl);
			}
		}
		
		String subjectImgUrl=shopInfo.getSubjectImgUrl();
		String newSubjectImgUrl=shopInfoEditVO.getSubjectImgUrl();
		
		if(StringUtils.isNotBlank(newSubjectImgUrl) && !newSubjectImgUrl.equals(subjectImgUrl)){
			//FileUtil.moveFile(uploadPath + newSubjectImgUrl, shopInfoPath);
			boolean is = AliyunOSSClientUtil.moveFile(backetName,newSubjectImgUrl, shopInfoPath, uploadPath);
			if(is){
				AliyunOSSClientUtil.removeFile(backetName, uploadPath+newSubjectImgUrl);
			}
		}
		
		Shop param=new Shop();
		param.setId(shopInfo.getShopId());
		param.setAuditStatus(AuditStatus.AUDIT.toIntStatus());
		param.setStatus(ShopStatus.STOP.toIntStatus());
		param.setIsRecentChange(Is.YES.toIntValue());
		shopDAO.update(param);
		
		
		return result;
	}
	
	@Override
	public ResultVO delShopInfoImg(Integer imgType,String id){
		ResultVO result=new ResultVO();
		ShopInfo shopInfo=shopInfoDAO.query(id);
		switch(imgType){
			case 0:
				//FileUtil.removeFile(shopInfoPath+shopInfo.getIdentityFrontImgUrl());
				AliyunOSSClientUtil.removeFile(backetName, shopInfoPath+shopInfo.getIdentityFrontImgUrl());
				shopInfo.setIdentityFrontImgUrl(null);
				shopInfoDAO.update(shopInfo,true);
				break;
			case 1:
				//FileUtil.removeFile(shopInfoPath+shopInfo.getIdentityBackImgUrl());
				AliyunOSSClientUtil.removeFile(backetName, shopInfoPath+shopInfo.getIdentityBackImgUrl());
				shopInfo.setIdentityBackImgUrl(null);
				shopInfoDAO.update(shopInfo,true);
				break;
			case 2:
				//FileUtil.removeFile(shopInfoPath+shopInfo.getIdentityImgUrl());
				AliyunOSSClientUtil.removeFile(backetName, shopInfoPath+shopInfo.getIdentityImgUrl());
				shopInfo.setIdentityImgUrl(null);
				shopInfoDAO.update(shopInfo,true);
				break;
			case 3:
				//FileUtil.removeFile(shopInfoPath+shopInfo.getSubjectImgUrl());
				AliyunOSSClientUtil.removeFile(backetName, shopInfoPath+shopInfo.getSubjectImgUrl());
				shopInfo.setSubjectImgUrl(null);
				shopInfoDAO.update(shopInfo,true);
				break;
		}
		
		return result;
		
	}
	
	@Override
	public ResultVO findByShopId(String shopId){
		ResultVO result=new ResultVO();
		ShopInfo data=shopInfoDAO.findByShopId(shopId);
		if(data==null){
			throw new ShopInfoNotFoundException("营业执照信息是不存在的");
		}
		result.setData(data);
		return result;
	}

}
